package com.example.nongye.utils;


import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    public static final String DATE_FORMAT = "yyyy-MM-dd";

    /**
     * String 转 Data
     *
     * @param strDate
     * @return
     */
    public static Date stringToDate(String strDate) {
        if (TextUtils.isEmpty(strDate)) {
            return null;
        } else {
            return stringToDate(strDate, DATE_FORMAT);
        }
    }

    /**
     * String 转 Data
     *
     * @param strDate
     * @param format
     * @return
     */
    public static Date stringToDate(String strDate, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(strDate);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * Date转字符创
     *
     * @param data
     * @param formatType
     * @return
     */
    public static String dateToString(Date data, String formatType) {
        return new SimpleDateFormat(formatType).format(data);
    }


    /**
     * 时间戳转Date
     *
     * @param currentTime
     * @param formatType
     * @return
     * @throws ParseException
     */
    public static Date longToDate(long currentTime, String formatType)
            throws ParseException {
        Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
        String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
        Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
        return date;
    }

    /**
     * 获取年龄
     *
     * @param birthDate
     * @return
     */
    public static String getAge(Date birthDate) {
        String str = "";
        if (null != birthDate) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy");
            int now = Integer.parseInt(format.format(new Date(System.currentTimeMillis())));
            int birth = Integer.parseInt(format.format(birthDate));
            str = String.valueOf(now - birth);
        }
        return str;
    }

    /**
     * 根据年月日获取Date
     *
     * @param year
     * @param monthOfYear
     * @param dayOfMonth
     * @return
     * @throws ParseException
     */
    public static Date getDate(int year, int monthOfYear, int dayOfMonth) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
        return format.parse(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
    }


    /**
     * 是否超过1年
     *
     * @param start
     * @param endCalendar
     * @return
     */
    public static boolean isYearMore(Date start, Calendar endCalendar) {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(start);
        endCalendar.add(Calendar.YEAR, -1);
        if (endCalendar.after(startCalendar)) {
            endCalendar.add(Calendar.YEAR, 1);
            return true;
        } else {
            endCalendar.add(Calendar.YEAR, 1);
            return false;
        }
    }

    public static boolean isYearMore(Calendar startCalendar, Date end) {
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(end);
        endCalendar.add(Calendar.YEAR, -1);
        if (endCalendar.after(startCalendar)) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * end 是否在start后
     *
     * @param start
     * @param endCalendar
     * @return
     */
    public static boolean isAfter(Date start, Calendar endCalendar) {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(start);
        if (endCalendar.after(startCalendar)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * end 是否在start后
     *
     * @param startCalendar
     * @param end
     * @return
     */
    public static boolean isAfter(Calendar startCalendar, Date end) {
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(end);
        if (endCalendar.after(startCalendar)) {
            return true;
        } else {
            return false;
        }
    }
}
